<template>
  <div class="login-container">
    <!-- 登录表单 -->
    <el-form ref="loginForm" :model="loginForm"
             :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <!-- 登录页面标题 -->
      <div class="title-container">
        <h3 class="title">登 录</h3>
      </div>

      <!-- 账号输入 -->
      <el-form-item prop="userid">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="userid"
          v-model="loginForm.userid"
          placeholder="请输入账号"
          name="userid"
          :maxlength="11"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <!-- 密码输入 -->
      <el-form-item prop="password">
        <!-- 输入框图标 -->
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <!-- 密码是否可见 -->
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary"
                 style="width:100%;margin-bottom:30px;"
                 @click.native.prevent="handleLogin">登录</el-button>

      <div class="skip_a">
        <router-link class="forget" to=""  >忘记密码</router-link>
        <router-link class="register" to="/register" >注册账号</router-link>
      </div>

    </el-form>
  </div>
</template>

<script>
  import {validUserid, validUserPass} from '@/utils/validate'
  import {idUnique} from "@/api/user";

export default {
  name: 'Login',
  data() {
    // 校验账号
    const validateUserid =async (rule,value,callback)=>{
      if (!validUserid(value)) {
        callback(new Error('请输入合法账号 （5-11个数字 不能以0开头）'));
      } else {
        // 校验账号是否已经存在
        await idUnique(value).then(res=>{
          if(res.content.result){
            callback(new Error("该账号还未注册，请先去注册！"))
          }else {
            callback();
          }
        })
      }
    }
    // 校验第一个密码
    const validatePassword = (rule, value, callback) => {
      if (!validUserPass(value)) {
        callback(new Error('密码由 6-15个大小写字母或数字组成'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        userid: '',
        password: ''
      },
      loginRules: {
        userid: [{ required: true, trigger: 'blur', validator: validateUserid }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    // 监听$route对象的变化，并指定immediate属性为true，表示立即执行监听器
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
        console.log(route)
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            console.log(this.redirect)
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }



  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
  .skip_a {
    text-align: center;
    color: $light_gray;
    font-size: 14px;
  }
  .skip_a .forget{
    margin-right: 35px;
  }
  .skip_a .register{
    margin-left: 35px;
  }
 .skip_a .forget:hover{
   color: #20a0ff;
  }
 .skip_a .register:hover{
  color: #20a0ff;
  }

</style>
